package ru.osu.acm.common.industrial;

import ru.osu.acm.common.entities.Submission;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * User: aleksey
 * Date: Sep 22, 2009
 * Time: 10:39:35 AM
 */
public class SubmissionFactory extends Factory {
    public static ArrayList<Submission> getContestSubmissions(int contestId, Connection con, boolean closeConnection) {
        ArrayList<Submission> submissions = new ArrayList<Submission>();

        try {
            PreparedStatement statement = con.prepareStatement("select * from submission where contest = ? order by id desc");
            statement.setInt(1, contestId);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                submissions.add(new Submission(
                        resultSet.getInt("id"),
                        PersonFactory.getPerson(resultSet.getInt("person"), con, false),
                        TeamFactory.getTeam(resultSet.getInt("team"), con, false),
                        ProblemFactory.getProblem(resultSet.getInt("problem"), con, false),
                        resultSet.getInt("status"),
                        resultSet.getInt("plagStatus"),
                        resultSet.getDouble("plagCoeff"),
                        resultSet.getString("compiler").trim(),
                        resultSet.getInt("testtime"),
                        resultSet.getInt("test"),
                        resultSet.getInt("contest"),
                        resultSet.getTimestamp("submitted"),
                        resultSet.getString("source").trim()));
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return submissions;
    }

    public static ArrayList<Submission> getSubmissionsByStatus(int status, Connection con, boolean closeConnection) {
        ArrayList<Submission> submissions = new ArrayList<Submission>();

        try {
            PreparedStatement statement = con.prepareStatement("select * from submission where status = ? order by id");
            statement.setInt(1, status);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                submissions.add(new Submission(
                        resultSet.getInt("id"),
                        PersonFactory.getPerson(resultSet.getInt("person"), con, false),
                        TeamFactory.getTeam(resultSet.getInt("team"), con, false),
                        ProblemFactory.getProblem(resultSet.getInt("problem"), con, false),
                        resultSet.getInt("status"),
                        resultSet.getInt("plagStatus"),
                        resultSet.getDouble("plagCoeff"),
                        resultSet.getString("compiler").trim(),
                        resultSet.getInt("testtime"),
                        resultSet.getInt("test"),
                        resultSet.getInt("contest"),
                        resultSet.getTimestamp("submitted"),
                        resultSet.getString("source").trim()));
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return submissions;
    }

    public static ArrayList<Submission> getSubmissionsForPlagTesting(int plagStatus, int status, Connection con, boolean closeConnection) {
        ArrayList<Submission> submissions = new ArrayList<Submission>();

        try {
            PreparedStatement statement = con.prepareStatement("select * from submission where plagStatus = ? and status = ? order by id");
            statement.setInt(1, plagStatus);
            statement.setInt(2, status);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                submissions.add(new Submission(
                        resultSet.getInt("id"),
                        PersonFactory.getPerson(resultSet.getInt("person"), con, false),
                        TeamFactory.getTeam(resultSet.getInt("team"), con, false),
                        ProblemFactory.getProblem(resultSet.getInt("problem"), con, false),
                        resultSet.getInt("status"),
                        resultSet.getInt("plagStatus"),
                        resultSet.getDouble("plagCoeff"),
                        resultSet.getString("compiler").trim(),
                        resultSet.getInt("testtime"),
                        resultSet.getInt("test"),
                        resultSet.getInt("contest"),
                        resultSet.getTimestamp("submitted"),
                        resultSet.getString("source").trim()));
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return submissions;
    }

    public static ArrayList<Submission> getPlagSubmissionsForSubmission(Submission submission, int plagStatus, int status, Connection con, boolean closeConnection) {
        ArrayList<Submission> submissions = new ArrayList<Submission>();

        try {
            PreparedStatement statement = con.prepareStatement("select b.* from submission a, submission b where a.id = ? and a.plagStatus = ? and b.status = ? and b.person <> a.person and a.problem = b.problem and b.id < a.id order by id");
            statement.setInt(1, submission.getId());
            statement.setInt(2, plagStatus);
            statement.setInt(3, status);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                submissions.add(new Submission(
                        resultSet.getInt("id"),
                        PersonFactory.getPerson(resultSet.getInt("person"), con, false),
                        TeamFactory.getTeam(resultSet.getInt("team"), con, false),
                        ProblemFactory.getProblem(resultSet.getInt("problem"), con, false),
                        resultSet.getInt("status"),
                        resultSet.getInt("plagStatus"),
                        resultSet.getDouble("plagCoeff"),
                        resultSet.getString("compiler").trim(),
                        resultSet.getInt("testtime"),
                        resultSet.getInt("test"),
                        resultSet.getInt("contest"),
                        resultSet.getTimestamp("submitted"),
                        resultSet.getString("source").trim()));
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return submissions;
    }
}
